//
//  MBComDetailController.m
//  MiBaoTu
//
//  Created by 彭鹏 on 16/9/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBComDetailController.h"

@interface MBComDetailController ()<UIWebViewDelegate> {
    UIWebView           *_webView;
}

@end

@implementation MBComDetailController

- (void)viewDidLoad {
    [super viewDidLoad];

//    [self setTitleView:@"比赛详情"];
    [self setupNavBar];
    
    [self onCreateUI];
}

- (void)setupNavBar {
    
    [self setBackItem];
}

- (void)onCreateUI
{
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.view.height)];
    if (ISIPAD) {
        _webView.frame=CGRectMake(460*PERCEN, 0, SCREEN_WIDTH-2*460*PERCEN, SCREEN_HEIGHT);
    }
    _webView.backgroundColor = [UIColor clearColor];
    _webView.delegate = self;
    _webView.scalesPageToFit = YES;
    [self.view addSubview:_webView];

    if (_contentName.length > 0) {
        NSArray *contentArr = [_contentName componentsSeparatedByString:@"<img"];
        NSMutableArray *picArr = [[NSMutableArray alloc]init];
        for (NSString *content in contentArr) {
            if ([content hasPrefix:@" src="]) {
                if ([content rangeOfString:@" title="].location != NSNotFound) {
                    NSMutableString *picName = [NSMutableString stringWithString:[content componentsSeparatedByString:@" title="][0]];
                    [picName replaceOccurrencesOfString:@" src=\"" withString:@"" options:1 range:NSMakeRange(0, picName.length)];
                    [picName replaceOccurrencesOfString:@"\"" withString:@"" options:1 range:NSMakeRange(0, picName.length)];
                    [picArr addObject:picName];
                }
            }
        }
        
        NSMutableString *mutStr = [[NSMutableString alloc]initWithString:_contentName];
        for (NSString *imgStr in picArr) {
            NSString *newImgStr = [NSString stringWithFormat:@"%@%@",MUSIC_PLAY_Url,imgStr];
            NSRange range = [mutStr rangeOfString:imgStr];
            [mutStr replaceCharactersInRange:range withString:newImgStr];
        }
        
        NSString * htmlcontent = [NSString stringWithFormat:@"<div id=\"webview_content_wrapper\">%@</div>", mutStr];
        [_webView loadHTMLString:htmlcontent baseURL:nil];
    }

}

-(void)navigationBackButton {
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    return YES;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    
}

@end
